﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Data.SqlClient;
using Orf.dto;
using Orf.Model;
namespace Orf_system
{
    public partial class FrmLogin : Form
    {
        employeeInfoDAO empDAO = null;
        DIC_YGDM empClass = null;
        DataTable dt = null;
        FrmhuaJiaSf frmHjsf = new FrmhuaJiaSf();
        FrmMain frmMain = new FrmMain();
        public String action = "login";//lock
        public string empName = null;
        public static string emp_sn = null;
        public string loginName = null;
        public string loginTime = null;
        public string empPwd = null;
        public int res;

        public FrmLogin()
        {
            InitializeComponent();
            frmMain.SkinFile(Properties.Resources.MP10);
            empDAO = new employeeInfoDAO();
            empClass = new DIC_YGDM();
            dt = new DataTable();

            this.ActiveControl = txtUserName;
            this.KeyPreview = true;
        }

        //用回车完成Tab的功能，使用onkeypress
        protected override void OnKeyPress(KeyPressEventArgs e)
        {
            if (e.KeyChar == 13)
                this.SelectNextControl(this.ActiveControl, true, true, true, true);
            base.OnKeyPress(e);
        }

        private void FrmLogin_Load(object sender, EventArgs e)
        {
            txtUserName.Focus();
        }

        /// <summary>
        /// 清空密码
        /// </summary>
        internal void clearPassword()
        {
            txtPwd.Text = "";
        }

        private void btnExit_Click(object sender, EventArgs e)
        {
            if (action == "lock")
            {
                labStatus.Text = "本系统已被" + empName + "锁定";
                return;
            }
            this.Close();
        }

        private void btnOK_Click(object sender, EventArgs e)
        {
            if (txtUserName.Text == "")
            {
                MessageBox.Show("请输入用户名！");
                txtUserName.Focus();
            }

            if (txtPwd.Text == "")
            {
                MessageBox.Show("请输入密码！");
                txtPwd.Focus();
            }

            if (!backgroundWorker1.IsBusy)
            {
                backgroundWorker1.RunWorkerAsync();
            }
        }

        private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
        {
            empClass.loginName = txtUserName.Text;
            empClass.pWD = txtPwd.Text;
            try
            {
                dt = empDAO.selectEmpInfo(empClass);
                res = dt.Rows.Count;
            }
            catch (Exception)
            {
                throw;
            }
        }

        private void backgroundWorker1_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
        {
            if (res != 0 && action == "login")
            {
                foreach (DataRow dr in dt.Rows)
                {
                    empName = dr[2].ToString();
                    emp_sn = dr[0].ToString();
                    loginName = dr[1].ToString();
                    empPwd = dr[4].ToString();
                }
                DateTime dateTime = DateTime.Now;
                loginTime = dateTime.ToShortDateString() + " " + dateTime.ToShortTimeString();
                txtPwd.Text = "";
                this.Hide();
                this.Visible = false;
                frmMain.login = this;
                frmMain.Show();
            }
            //else
            //{
            //    MessageBox.Show("用户或密码错误！", "提示");
            //    txtPwd.Text = "";
            //    txtUserName.Focus();
            //}

            if (action == "lock" && frmMain != null)//判断是否锁定系统了，是的话输入当前用户的密码后就转回主界面
            {
                this.frmMain.Show();
                this.Hide();
            }
        }
    }
}
